<template>
	<view v-if="goodsInfo.goods_name">
		<!-- 轮播图区域 -->
		<swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000"circular="true">
			<swiper-item v-for="(item, i) in goodsInfo.pics" :key="i" @click="preImage(i)">
				<view class="swiper-item">
					<image :src="item.pics_big"></image>
				</view>
			</swiper-item>
		</swiper>
		<!-- 商品详情区域 -->
		<view class="goods-info-box">
			<view class="goods-price">￥{{goodsInfo.goods_price}}</view>
			<view class="goods-title">
				<view class="goods-name">{{goodsInfo.goods_name}}</view>
				<view class="goods-title-right">
					<uni-icons type="star" size="20" color="gray"></uni-icons>
					<text class="favi">收藏</text>
				</view>
			</view>
			<view class="yunfei">快递：免运费</view>
		</view>
		<!--展示详情-->
		<view class="goods-detail-container">
			<rich-text :nodes="goodsInfo.goods_introduce"></rich-text>
		</view>
		<!-- 购买导航区域 -->
		<view class="nav-goods">
			<uni-goods-nav :fill="true"  :options="options" :buttonGroup="buttonGroup"  @click="onClick" @buttonClick="buttonClick" />
		</view>
	</view>
</template>

<script>
	import {mapMutations,mapGetters} from "vuex"
	export default {
		computed:{
			...mapGetters('my_cart',['total'])
		},
		watch:{
			//定义total侦听器，指向一个配置对象
			total:{
				//handler属性用来定义侦听器的function处理函数
				handler(newVal){
					const findResult = this.options.find(x => x.text === '购物车')
					if(findResult){
						findResult.info = newVal
					}
				},
				//immediate属性用来声明侦听器，是否在页面初次加载完毕后立即调用
				immediate:true
			}
		},
		data() {
			return {
				goodsInfo:{},
				options: [{
							icon: 'shop',
							text: '店铺',
							infoBackgroundColor:'#007aff',
							infoColor:"red"
						}, {
							icon: 'cart',
							text: '购物车',
							info: 0
						}],
					    buttonGroup: [{
					      text: '加入购物车',
					      backgroundColor: '#ff0000',
					      color: '#fff'
					    },
					    {
					      text: '立即购买',
					      backgroundColor: '#ffa200',
					      color: '#fff'
					    }
					    ]
			};
		},
		onLoad(options){
			this.getGoodsInfo(options.goods_id)
		},
		methods:{
			...mapMutations('my_cart',['addToCart']),
			async getGoodsInfo(goods_id){
				const {data:res} = await uni.$http.get("/api/public/v1/goods/detail",{goods_id})
				if(res.meta.status !== 200) return uni.$showMsg()
				// 使用字符串的 replace() 方法，将 webp 的后缀名替换为 jpg 的后缀名
				res.message.goods_introduce = res.message.goods_introduce.replace(/<img /g,
				'<img style="display:block;" ').replace(/webp/g, 'jpg')
				this.goodsInfo = res.message
			},
			preImage(i){
				uni.previewImage({
					current:i,
					urls:this.goodsInfo.pics.map(x => x.pics_big_url)
				})
			},
			onClick(){
				uni.switchTab({
					url:"/pages/cart/cart"
				})
			},
			buttonClick(e){
				if(e.content.text === '加入购物车'){
					//封装一个购物车商品对象
					const goods = {
						goods_id:this.goodsInfo.goods_id,//商品id
						goods_name:this.goodsInfo.goods_name,//商品名称
						goods_price:this.goodsInfo.goods_price,//商品价格
						goods_count:1,//商品数量
						goods_small_logo:this.goodsInfo.goods_small_logo,//商品图片
						goods_state:true//商品默认为勾选状态
					}
					this.addToCart(goods)
				}
			}
		}
	}
</script>

<style lang="scss">
swiper {
		height: 750rpx;
		.swiper-item{
			height: 100%;
			image {
				width: 100%;
				height: 100%;
			}
		}
}
.goods-info-box{
	padding: 8px;
	padding-right: 0;
	.goods-price{
		color: #F00000;
	}
	.goods-title{
		display: flex;
		height: 35px;
		padding: 10px 0;
		.goods-name{
			font-size: 12px;
			padding-right: 10px;
			overflow: hidden;
			display: -webkit-box;
			text-overflow: ellipsis;
			-webkit-box-orient: vertical;
			-webkit-line-clamp: 2;
		}
		.goods-title-right{
			display: flex;
			width: 120px;
			flex-direction: column;
			align-items: center;
			border-left: 1px solid #f0f0f0;
			.favi{
				font-size: 12px;
				color: gray;
			}
		}
	}
	.yunfei{
		font-size: 13px;
		color: grey;
		border-bottom: 1px solid #f0f0f0;
	}
}
.goods-detail-container{
	padding-bottom: 50px;
}
.nav-goods{
	position: fixed;
	width: 100%;
	bottom: 0;
	left: 0;
}
</style>
